package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.InvoiceDao;
import cn.itcast.dao.cargo.ShippingDao;
import cn.itcast.domain.cargo.Invoice;
import cn.itcast.domain.cargo.InvoiceExample;
import cn.itcast.domain.cargo.Shipping;
import cn.itcast.service.cargo.InvoiceService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class InvoiceServiceImpl implements InvoiceService {

    //注入发票Dao
    @Autowired
    private InvoiceDao invoiceDao;

    //注入委托单
    @Autowired
    private ShippingDao shippingDao;


    /**
     * 分页查询
     * @param invoiceExample
     * @param pageNum
     * @param pageSize
     */
    @Override
    public PageInfo<Invoice> findByPage(InvoiceExample invoiceExample, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        return new PageInfo<>(invoiceDao.selectByExample(invoiceExample));
    }

    /**
     * 查询所有
     *
     * @param invoiceExample
     */
    @Override
    public List<Invoice> findAll(InvoiceExample invoiceExample) {
        return invoiceDao.selectByExample(invoiceExample);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public Invoice findById(String id) {
        return invoiceDao.selectByPrimaryKey(id);
    }

    /**
     * 新增
     *
     * @param invoice
     */
    @Override
    public void save(Invoice invoice) {
        //拿到发票id
        String invoiceId = invoice.getInvoiceId();
        //根据发票id查出委托单，修改委托单状态
        Shipping shipping = shippingDao.selectByPrimaryKey(invoiceId);
        shipping.setState(1);
        shippingDao.updateByPrimaryKeySelective(shipping);

        invoiceDao.insertSelective(invoice);
    }

    /**
     * 修改
     *
     * @param invoice
     */
    @Override
    public void update(Invoice invoice) {
        invoiceDao.updateByPrimaryKeySelective(invoice);
    }

    /**
     * 删除部门
     *
     * @param id
     */
    @Override
    public void delete(String id) {
        invoiceDao.deleteByPrimaryKey(id);
    }
}
